home *** CD-ROM | disk | FTP | other *** search
/ Freelog 22 / freelog 22.iso / Prog / Djgpp / GPC2952B.ZIP / lib / gcc-lib / djgpp / 2.952 / units / dosunix.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-02-08  |  10.3 KB  |  325 lines

  1. {
  2. Some routines to support writing programs portable between Dos and
  3. Unix. Perhaps it would be a good idea not to put features to make
  4. Dos programs Unix-compatible (shell redirections) and vice versa
  5. (reading Dos files from Unix) together into one unit, but rather
  6. into two units, DosCompat and UnixCompat or so -- let's wait and
  7. see, perhaps when more routines suited for this/these unit(s) will
  8. be found, the design will become clearer...
  9.  
  10. Copyright (C) 1998-2001 Free Software Foundation, Inc.
  11.  
  12. Author: Frank Heckenbach <frank@pascal.gnu.de>
  13.  
  14. This file is part of GNU Pascal.
  15.  
  16. GNU Pascal is free software; you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License as published by
  18. the Free Software Foundation; either version 2, or (at your option)
  19. any later version.
  20.  
  21. GNU Pascal is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. GNU General Public License for more details.
  25.  
  26. You should have received a copy of the GNU General Public License
  27. along with GNU Pascal; see the file COPYING. If not, write to the
  28. Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  29. 02111-1307, USA.
  30.  
  31. As a special exception, if you link this file with files compiled
  32. with a GNU compiler to produce an executable, this does not cause
  33. the resulting executable to be covered by the GNU General Public
  34. License. This exception does not however invalidate any other
  35. reasons why the executable file might be covered by the GNU General
  36. Public License.
  37. }
  38.  
  39. {$gnu-pascal,B-,I-}
  40. {$if __GPC_RELEASE__ < 20000412}
  41. {$error This unit requires GPC release 20000412 or newer.}
  42. {$endif}
  43.  
  44. unit DosUnix;
  45.  
  46. interface
  47.  
  48. uses GPC;
  49.  
  50. {
  51.   This function is meant to be used when you want to invoke a system
  52.   shell command (e.g. via Execute or Exec from the Dos unit) and
  53.   want to specify input/output redirections for the command invoked.
  54.   It caters for the different syntax between DJGPP (with the `redir'
  55.   utility) and other systems.
  56.  
  57.   To use it, code your redirections in bash style (see the table
  58.   below) in your command line string, pass this string to this
  59.   function, and the function's result to Execute or the other
  60.   routines.
  61.  
  62.   The function translates the following bash style redirections
  63.   (characters in brackets are optional) into a redir call under Dos
  64.   systems except EMX, and leave them unchanged under other systems.
  65.   Note: `redir' comes with DJGPP, but it should be possible to
  66.   install it on other Dos systems as well. OS/2's shell, however,
  67.   supports bash style redirections, I was told, so we don't
  68.   translate on EMX.
  69.  
  70.   [0]<     file      redirect standard input from file
  71.   [1]>[|]  file      redirect standard output to file
  72.   [1]>>    file      append standard output to file
  73.   [1]>&2             redirect standard output to standard error
  74.   2>[|]    file      redirect standard error to file
  75.   2>>      file      append standard error to file
  76.   2>&1               redirect standard error to standard output
  77.   &> file            redirect both standard output and standard
  78.                      error to file
  79. }
  80. function TranslateRedirections (const Command : String) : TString;
  81.  
  82. { Under Unix, translates CR/LF pairs to single LF characters when
  83.   reading from f, and back when writing to f. Under Dos, does
  84.   nothing because the run time system alrady does this job. In the
  85.   result, you can read both Dos and Unix files, and files written
  86.   will be Dos. }
  87. procedure AssignDos (var f : AnyFile; const Name : String);
  88.  
  89. implementation
  90.  
  91. (*@@fjf252*)(*$local W-*)(*$ifdef DJGPP*)type sizetype=word;(*$endif*)(*$endlocal*)
  92.  
  93. function TranslateRedirections (const Command : String) = s : TString;
  94. {$if defined (__OS_DOS__) and not defined (__EMX__)}
  95. const
  96.   FileNameChars = ['A'..'Z', 'a'..'z', '0'..'9', '_', '/', '\', ':', '.', ',', '+', '-', '=', '!', '$', '?', '*', '[', ']', '~', '^', '%', '"', '''', '`', '#', #128..#255];
  97.  
  98. var
  99.   i, k : Integer;
  100.   Redir : TString;
  101.   Redirs : (RNone, ROut, RErr, RBoth, ROutErr, RErrOut);
  102.   AppendFlag : Boolean;
  103.   InString : Char;
  104.  
  105.   procedure GetFileName;
  106.   var j : Integer;
  107.   begin
  108.     j := k;
  109.     while (j <= Length (s)) and (s [j] in [' ', #9]) do Inc (j);
  110.     k := j;
  111.     while (k <= Length (s)) and (s [k] in FileNameChars) do Inc (k);
  112.     Redir := Redir + ' ' + Copy (s, j, k - j) + ' '
  113.   end;
  114.  
  115. begin
  116.   s := Command;
  117.   Redir := '';
  118.   InString := #0;
  119.   i := 1;
  120.   while i <= Length (s) do
  121.     begin
  122.       s [Length (s) + 1] := #0;
  123.       while (i <= Length (s)) and ((InString <> #0) or not (s [i] in ['<', '>'])) do
  124.         begin
  125.           if InString <> #0 then
  126.             if s [i] = InString then InString := #0 else
  127.           else if s [i] in ['"', ''''] then
  128.             InString := s [i];
  129.           Inc (i)
  130.         end;
  131.       if i <= Length (s) then
  132.         begin
  133.           if s [i] = '<' then
  134.             begin
  135.               k := i + 1;
  136.               if (i > 1) and (s [i - 1] = '0') then Dec (i);
  137.               Redir := Redir + '-i';
  138.               GetFileName
  139.             end
  140.           else
  141.             begin
  142.               Redirs := ROut;
  143.               AppendFlag := False;
  144.               k := i + 1;
  145.               if i > 1 then
  146.                 case s [i - 1] of
  147.                   '1' : Dec (i);
  148.                   '2' : begin
  149.                           Redirs := RErr;
  150.                           Dec (i)
  151.                         end;
  152.                   '&' : begin
  153.                           Redirs := RBoth;
  154.                           Dec (i)
  155.                         end;
  156.                 end;
  157.               if s [k] = '>' then
  158.                 begin
  159.                   AppendFlag := True;
  160.                   Inc (k)
  161.                 end;
  162.               if s [k] = '|' then Inc (k);
  163.               if s [k] = '&' then
  164.                 begin
  165.                   Inc (k);
  166.                   case s [k] of
  167.                     '1' : begin
  168.                             if Redirs = RErr
  169.                               then Redirs := RErrOut
  170.                               else Redirs := RNone;
  171.                             Inc (k)
  172.                           end;
  173.                     '2' : begin
  174.                             if Redirs = ROut
  175.                               then Redirs := ROutErr
  176.                               else Redirs := RNone;
  177.                             Inc (k)
  178.                           end;
  179.                     else Redirs := RBoth
  180.                   end
  181.                 end;
  182.               case Redirs of
  183.                 ROut,
  184.                 RErr,
  185.                 RBoth   : begin
  186.                             if Redirs = RErr
  187.                               then Redir := Redir + '-e'
  188.                               else Redir := Redir + '-o';
  189.                             if AppendFlag then Redir := Redir + 'a';
  190.                             GetFileName;
  191.                             if Redirs = RBoth then Redir := Redir + '-eo '
  192.                           end;
  193.                 ROutErr : Redir := Redir + '-oe ';
  194.                 RErrOut : Redir := Redir + '-eo ';
  195.               end
  196.             end;
  197.           Delete (s, i + 1, k - i - 1);
  198.           s [i] := ' '
  199.         end
  200.     end;
  201.   if Redir <> '' then s := 'redir ' + Redir + s
  202. end;
  203. {$else}
  204. begin
  205.   s := Command
  206. end;
  207. {$endif}
  208.  
  209. type
  210.   PAssignDosData = ^TAssignDosData;
  211.   TAssignDosData = record
  212.     f : File;
  213.     PendingChar : Integer
  214.   end;
  215.  
  216. procedure AssignDosOpen (var PrivateData; Mode : TOpenMode);
  217. var Data : TAssignDosData absolute PrivateData;
  218. begin
  219.   case Mode of
  220.     foRewrite : Rewrite (Data.f, 1);
  221.     foAppend  : Append  (Data.f, 1);
  222.     else        Reset   (Data.f, 1)
  223.   end
  224. end;
  225.  
  226. function AssignDosSelectFunc (var PrivateData; Writing : Boolean) : Integer;
  227. var Data : TAssignDosData absolute PrivateData;
  228. begin
  229.   AssignDosSelectFunc := FileHandle ((*@@*)anyfile( Data.f))
  230. end;
  231.  
  232. function AssignDosRead (var PrivateData; var Buffer; Size : SizeType) = BytesRead : SizeType;
  233. var
  234.   Data : TAssignDosData absolute PrivateData;
  235.   CharBuf : array [1 .. Size] of Char absolute Buffer;
  236.   i, j : SizeType;
  237.   Temp : Char;
  238. begin
  239.   repeat
  240.     BlockRead (Data.f, Buffer, (*@@fjfwhatever*)integer(   Size - Ord ((Size > 1) and (Data.PendingChar >= 0))), BytesRead);
  241.     if (InOutRes <> 0) or (BytesRead <= 0) then Exit;
  242.     if Data.PendingChar >= 0 then
  243.       if Size > 1 then
  244.         begin
  245.           for i := BytesRead downto 1 do CharBuf [i + 1] := CharBuf [i];
  246.           CharBuf [1] := Chr (Data.PendingChar);
  247.           Data.PendingChar := - 1;
  248.           Inc (BytesRead)
  249.         end
  250.       else if (Data.PendingChar = 13) and (CharBuf [1] = #10) then
  251.         Data.PendingChar := - 1
  252.       else
  253.         begin
  254.           Temp := Chr (Data.PendingChar);
  255.           Data.PendingChar := Ord (CharBuf [1]);
  256.           CharBuf [1] := Temp
  257.         end;
  258.     i := 1;
  259.     j := 0;
  260.     while (i < BytesRead) or ((i = BytesRead) and ((CharBuf [i] <> #13) or (Data.PendingChar >= 0))) do
  261.       begin
  262.         if (CharBuf [i] = #13) and (CharBuf [i + 1] = #10) then Inc (i);
  263.         Inc (j);
  264.         CharBuf [j] := CharBuf [i];
  265.         Inc (i)
  266.       end;
  267.     if i = BytesRead then Data.PendingChar := Ord (CharBuf [i]);
  268.     BytesRead := j
  269.   until BytesRead > 0
  270. end;
  271.  
  272. function AssignDosWrite (var PrivateData; const Buffer; Size : SizeType) = BytesWritten : SizeType;
  273. var
  274.   Data : TAssignDosData absolute PrivateData;
  275.   CharBuf : array [1 .. Size] of Char absolute Buffer;
  276.   NewBuf : array [1 .. 2 * Size] of Char;
  277.   i, j : Integer;
  278. begin
  279.   j := 0;
  280.   for i := 1 to Size do
  281.     begin
  282.       if CharBuf [i] = #10 then
  283.         begin
  284.           Inc (j);
  285.           NewBuf [j] := #13
  286.         end;
  287.       Inc (j);
  288.       NewBuf [j] := CharBuf [i]
  289.     end;
  290.   BlockWrite (Data.f, NewBuf, j, BytesWritten);
  291.   if (InOutRes = 0) and (BytesWritten > 0) then BytesWritten := Max (0, BytesWritten + Size - j)
  292. end;
  293.  
  294. procedure AssignDosFlush (var PrivateData);
  295. var Data : TAssignDosData absolute PrivateData;
  296. begin
  297.   Flush (Data.f)
  298. end;
  299.  
  300. procedure AssignDosClose (var PrivateData);
  301. var Data : TAssignDosData absolute PrivateData;
  302. begin
  303.   Close (Data.f)
  304. end;
  305.  
  306. procedure AssignDosDone (var PrivateData);
  307. var Data : TAssignDosData absolute PrivateData;
  308. begin
  309.   Dispose (@Data)
  310. end;
  311.  
  312. procedure AssignDos (var f : AnyFile; const Name : String);
  313. begin
  314.   Assign (f, Name);
  315.   {$ifndef __OS_DOS__}
  316.   var Data : PAssignDosData;
  317.   New (Data);
  318.   Data^.PendingChar := - 1;
  319.   Assign (Data^.f, Name);
  320.   AssignTFDD (f, AssignDosOpen, AssignDosSelectFunc, nil, AssignDosRead, AssignDosWrite, AssignDosFlush, AssignDosClose, AssignDosDone, Data)
  321.   {$endif}
  322. end;
  323.  
  324. end.
  325.